package com.shengqi.xinnongren.work.ui.user.activities;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.user.presenter.UpdatePwdPresenter;
import com.shengqi.xinnongren.work.ui.user.view.UpdatePwdView;

import butterknife.BindView;
import butterknife.OnClick;

public class UpdatePwdActivity extends BaseMvpActivity<UpdatePwdView, UpdatePwdPresenter> implements UpdatePwdView {

    @BindView(R.id.btn_save)
    Button mBtnSave;
    @BindView(R.id.et_pwd_old)
    EditText mEtPwdOld;
    @BindView(R.id.et_pwd_new)
    EditText mEtPwdNew;
    @BindView(R.id.et_pwd_new_confirm)
    EditText mEtPwdNewConfirm;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }


    @OnClick(R.id.btn_save)
    public void updatePwd() {
        String oldPwd = mEtPwdOld.getText().toString();
        if (TextUtils.isEmpty(oldPwd)) {
            ToastHelper.getInstance(this).showWarn("请输入原密码");
            return;
        }
        String p1 = mEtPwdNew.getText().toString();
        if (TextUtils.isEmpty(p1)) {
            ToastHelper.getInstance(this).showWarn("请输入新密码");
            return;
        }
        String p2 = mEtPwdNewConfirm.getText().toString();
        if (TextUtils.isEmpty(p2)) {
            ToastHelper.getInstance(this).showWarn("请再次输入新密码");
            return;
        }
        if (!p1.equals(p2)) {
            ToastHelper.getInstance(this).showWarn("两次输入的新密码不一致");
            return;
        }
        if (p1.length() < 6) {
            ToastHelper.getInstance(this).showWarn("密码长度最少6位");
            return;
        }
        mPresenter.changePwd(oldPwd, p1);
    }

    @Override
    protected UpdatePwdPresenter createPresenter() {
        return new UpdatePwdPresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {

    }


    @Override
    public void showUpdatePwdRes(HttpRespond<String> respond) {
        if (respond.result == 1) {
            ToastHelper.getInstance(this).showSucceed(respond.message);
            finish();
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }
}
